package com.hushao.aimi.controller;

import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 购物车控制层
 * @author: 胡少
 * @create: 2018-06-08 09:52
 **/
@RestController
@RequestMapping("/cart")
@Slf4j
@Api(value = "购物车服务")
public class CartController {

    @Autowired
    private CartService cartService;


    /**
     * 获取个人购物车列表
     * @param username
     * @return
     */
    @ApiOperation(value = "获取个人购物车列表")
    @GetMapping("/{username}")
    public ServerResponse getCartsByUsername(@PathVariable("username") String username){
        return cartService.getCartByUsername(username);
    }

    /**
     * 添加商品购物车到购物车列表
     * @param username
     * @param productSpecId
     * @param count
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "添加商品购物车到购物车列表")
    @PostMapping("/{username}")
    public ServerResponse add(@PathVariable("username") String username,String productSpecId,Integer count)throws Exception{
        return cartService.add(username,productSpecId,count);
    }

    /**
     * 修改商品购物车到购物车列表
     * @param cartId
     * @param count
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "添加商品购物车到购物车列表")
    @PutMapping("/{cartId}")
    public ServerResponse update(@PathVariable("cartId")String cartId,Integer count)throws Exception{
        return cartService.update(cartId,count);
    }

}
